home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / cbm / 890 < prev    next >
Encoding:
Text File  |  1996-08-05  |  8.3 KB  |  331 lines

  1. Path: news.hik.se!usenet
  2. From: Johan Ramestam <pt93jr@pt.hk-r.se>
  3. Newsgroups: comp.sys.cbm
  4. Subject: Converters for .D64/.T64
  5. Date: Tue, 16 Jan 1996 15:46:34 +0100
  6. Organization: Team Keso
  7. Message-ID: <30FBBA4A.1944@pt.hk-r.se>
  8. NNTP-Posting-Host: gemini.pt.hk-r.se
  9. Mime-Version: 1.0
  10. Content-Type: multipart/mixed; boundary="------------2290872267"
  11. X-Mailer: Mozilla 2.0b5 (X11; I; SunOS 5.4 sun4m)
  12.  
  13. This is a multi-part message in MIME format.
  14.  
  15. --------------2290872267
  16. Content-Type: text/plain; charset=us-ascii
  17. Content-Transfer-Encoding: 7bit
  18.  
  19. I attach the sourcecode for two converter programs; d64zipcode,
  20. written by Tony Ambardar, and tape2bin, written by someone 
  21. called Leon. The first programs splits a .D64 file into 4 zipcode
  22. files (1!filename, 2!filename...). The second program converts
  23. a .T64 file into a binary file which can be run on a 64. Enjoy!
  24.  
  25. /Johan
  26.  
  27. -----------------------------------
  28.  Johan Ramestam (pt93jr@pt.hk-r.se)
  29.  Software Engineering
  30.  University of Ronneby / Sweden
  31.  http://www.pt.hk-r.se/~pt93jr/
  32. -----------------------------------
  33.  
  34. --------------2290872267
  35. Content-Type: text/plain; charset=us-ascii
  36. Content-Transfer-Encoding: 7bit
  37. Content-Disposition: inline; filename="d64zipcode.c"
  38.  
  39. static char info[]=" D64 2 Zipcode   Version 1.0   Tony Ambardar (C) 1993\n";
  40. /*
  41. Wrote this mostly by looking through some zipcode 4-packs and playing with
  42. Zipcode 2.0, so that I could try .D64 software from the watson archive on
  43. my C64 at home. No guarantees.
  44.  
  45. You are free to re-use this code so long as you give credit where it's due.
  46.  
  47. (C) 1993     Tony Ambardar
  48. */
  49.  
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <string.h>
  53.  
  54. #define DBLOCKS (683)
  55.  
  56. typedef unsigned char byte;
  57. typedef enum { WHOLE=0, VARIABLE, NONE } repeat_t;
  58. static char repeat_c[]=".oO";
  59.  
  60. byte image_buffer[DBLOCKS*256],max_sector[35];
  61. static byte trk_lims[4] = {0, 8, 16, 25};
  62.  
  63. /* Per sector info */
  64.  
  65. int num_repeats;
  66. byte repchar;
  67. int rle_len;
  68. struct 
  69. {
  70.   int startpos;
  71.   int len;
  72. } rep_info[64];
  73.  
  74. static char filename[255] = "0!";
  75.  
  76. FILE *imgfile,*zipfile;
  77.  
  78. main(int argc, char *argv[]) 
  79. {
  80.   repeat_t rep_scan();
  81.   byte *sec_buf;
  82.   int i,j,k,track, sector,base;
  83.   int oddadd,evenadd,begin,length;
  84.   int pos;
  85.    
  86.   repeat_t sec_type;
  87.    
  88.   if(argc != 2) {
  89.     printf("%s",info);
  90.     printf("Converts a .D64 disk image to a Zipcode 4-pack\n\n");      
  91.     printf("Usage: %s  diskimage.d64\n",argv[0]);
  92.     exit(0);
  93.   }
  94.  
  95.   init_stuff(argv[1]);
  96.  
  97.   if ((pos = strcspn(argv[1], ".d64")) == 0)
  98.     pos = strcspn(argv[1], ".D64");
  99.  
  100.   argv[1][pos] = 0;
  101.  
  102.   zipfile = NULL;
  103.   strcat(filename,argv[1]);
  104.   printf("%s\n", filename);
  105.   base=0;
  106.   oddadd=11, evenadd= -10;
  107.   printf("\n            RLE Compression Map:\n");
  108.   printf("O = No Compression  -->  . = Max. Compression\n");
  109.   for(track=0; track<35; base+=max_sector[track++]+1) {
  110.     printf("\nTrack: %2u ",track+1);
  111.     openfiles(track,filename);
  112.     if(track==17 || track==24) --oddadd, ++evenadd;
  113.     i=base-evenadd;
  114.     sector= -evenadd;
  115.     for(k=0; k <= max_sector[track]; k++) {
  116.       i+= k % 2 ? oddadd : evenadd;
  117.       sec_buf = image_buffer +i*256;
  118.       sector += k % 2 ? oddadd : evenadd;
  119.       sec_type = rep_scan(sec_buf);
  120.       printf("%c",repeat_c[sec_type]);
  121.  
  122. #ifdef EBUG1
  123.       printf("\nT: %2u S: %2u Type: %c  Repchar: %3u\n",track+1,sector,
  124.          repeat_c[sec_type],repchar);
  125. #endif
  126.  
  127. #ifdef EBUG2
  128.       for(j=0;j<256;j++)
  129.     printf("\\%3o",*(sec_buf+j));
  130. #endif   
  131.  
  132.       if(sec_type==NONE) {
  133.     fputc(track+1,zipfile),fputc(sector,zipfile);
  134.     fwrite((sec_buf),1,256,zipfile);
  135.       }
  136.       else if(sec_type==WHOLE) {
  137.     fputc(track+1+64,zipfile),fputc(sector,zipfile);
  138.     fputc(*(sec_buf),zipfile);
  139.       }
  140.       else {            /* VARIABLE */
  141.     fputc(track+1+128,zipfile),fputc(sector,zipfile);
  142.     fputc(rle_len,zipfile),fputc(repchar,zipfile);
  143.     for(j=0; j<num_repeats; j++) {
  144.       begin= j==0? 0 : 
  145.       rep_info[j-1].startpos+rep_info[j-1].len;
  146.       length = rep_info[j].startpos - begin;
  147.       fwrite((sec_buf+begin),1,length,zipfile);
  148.       fputc(repchar,zipfile),
  149.       fputc(rep_info[j].len,zipfile);
  150.       fputc(*(sec_buf+rep_info[j].startpos),zipfile);
  151.     }
  152.     begin=rep_info[j-1].startpos+rep_info[j-1].len;
  153.     length = 256 - begin;
  154.     fwrite((sec_buf+begin),1,length,zipfile);
  155.       }
  156.     }
  157.   }
  158.   printf("\n\nDone.\n");
  159.   close(zipfile);
  160. }
  161.  
  162. openfiles(int trk,char *name)
  163. {
  164.   int i;
  165.   for(i=0; i<4; i++)
  166.     if(trk_lims[i] == trk) {
  167.       if(zipfile != NULL) fclose(zipfile);
  168.       ++*name;
  169.       if(!(zipfile = fopen(name,"w"))) {
  170.     perror(name);
  171.     exit(errno);
  172.       }
  173.       /*  Write headers. \041 \041 is a disk ID I think */
  174.             
  175.       if(i==0) fputs("\376\003\041\041",zipfile);
  176.       else fprintf(zipfile,"%c%c",0,4);
  177.     }
  178. }
  179.  
  180.  
  181. init_stuff(char *name)
  182. {
  183.   int i;
  184.  
  185.   for(i=0; i<17; max_sector[i++]=20);
  186.   for(i=17; i<24; max_sector[i++]=18);
  187.   for(i=24; i<30; max_sector[i++]=17);
  188.   for(i=30; i<35; max_sector[i++]=16);
  189.    
  190.   if(!(imgfile = fopen(name,"r"))) {
  191.     perror(name);
  192.     exit(errno);
  193.   }
  194.   else {
  195.     if(fread(image_buffer,1,sizeof(image_buffer),imgfile) 
  196.        != sizeof(image_buffer)) {
  197.       fprintf(stderr,"D64 image could be wrong size!\n");
  198.       exit(1);
  199.     }
  200.     close(imgfile);
  201.   }
  202. }
  203.  
  204. repeat_t rep_scan(byte *sec_buf)
  205. {
  206.   int i,j,count;
  207.   char table[256];
  208.  
  209.   rle_len=256;
  210.   num_repeats=0;
  211.       
  212.   for(i=0; i<255; i++) {
  213.     count=0;
  214.     while(i<255 && *(sec_buf+i)==*(sec_buf+i+1)) {
  215.       i++,count++;
  216.     }
  217.     if(count >= 3) {
  218.       rep_info[num_repeats].startpos=i-count;
  219.       rep_info[num_repeats].len=count+1;
  220.       ++num_repeats;
  221.     }         
  222.   }
  223.   if(num_repeats==0) return NONE;
  224.   else
  225.     if(count==255) return WHOLE;
  226.  
  227.   /*  VARIABLE  */
  228.  
  229.   rle_len += num_repeats*3;
  230.   for(i=0; i< num_repeats; i++)
  231.     rle_len -= rep_info[i].len;
  232.   if(rle_len >253) return NONE;
  233.       
  234.   for(j=0; j<256; table[j++]=1);
  235.   for(j=0; j<256;  table[*(sec_buf+j++)]=0);
  236.   for(j=0; j<256 && table[j]==0; j++);
  237.   repchar = j;
  238.    
  239. #ifdef EBUG1
  240.   printf("\nRepeats: %2u RLE_len: %3u Repchar: %3u\n",i,rle_len,j);
  241.   for(j=0;j<i;j++)
  242.     printf("Start: %3u Length: %3u\n",rep_info[j].startpos,rep_info[j].len);
  243. #endif   
  244.   return VARIABLE;   
  245. }
  246.  
  247. --------------2290872267
  248. Content-Type: text/plain; charset=us-ascii
  249. Content-Transfer-Encoding: 7bit
  250. Content-Disposition: inline; filename="tape2bin.c"
  251.  
  252. /*******************************************************************/
  253. /** Tape2Bin                                                      **/
  254. /** Extracts the first program in a .t64 file and stores it as a  **/
  255. /** binary file that should be loadable by a real C64 or A64 for  **/
  256. /** the Amiga.  Note, this won't work if the .t64 file has more   **/
  257. /** than one file in it.                                          **/
  258. /**                                                               **/
  259. /** All I did was convert the pascal source to C.  If I get time  **/
  260. /** I'll covert the other utilities and expand this one to do     **/
  261. /** multiple files.  This source SHOULD compile on any C compiler **/
  262. /** but I've only tested it with SAS C for the Amiga.             **/
  263. /**                                                               **/
  264. /**                                     Leon                      **/
  265. /*******************************************************************/
  266.  
  267. #include <stdio.h>
  268. #include <errno.h>
  269.  
  270. long Temp_Integer;
  271. unsigned char My_Byte;
  272. FILE *Input_File, *Output_File;
  273.  
  274. int main(int argc, char **argv) 
  275. {
  276.   int l = 0;
  277.   char buf[256];
  278.  
  279.   if (argc < 2) 
  280.   {
  281.     fprintf(stderr, "Format: %s <Input_File.T64>\n", argv[0]);
  282.     exit(0);
  283.   }
  284.   
  285.   if ((Input_File = fopen(argv[1], "rb")) == NULL)
  286.   {
  287.     perror("Can't open input file");
  288.     exit(errno);
  289.   }
  290.   
  291.   if ((l = strcspn(argv[1], ".t64")) == 0)
  292.     l = strcspn(argv[1], ".T64");
  293.   
  294.   argv[1][l] = 0;
  295.  
  296.   if ((Output_File = fopen(argv[1], "wb")) == NULL)
  297.   {
  298.     perror("Can't create output file");
  299.     exit(errno);
  300.   }
  301.   
  302.   if(fseek(Input_File, 0x42, 0)) /* reading back the first two bytes */
  303.   { 
  304.     perror("Error on fseek");
  305.     exit(errno);
  306.   }
  307.  
  308.   fread(&My_Byte, sizeof(unsigned char), 1, Input_File);
  309.   fwrite(&My_Byte, sizeof(unsigned char), 1, Output_File);
  310.   fread(&My_Byte, sizeof(unsigned char), 1, Input_File);
  311.   fwrite(&My_Byte, sizeof(unsigned char), 1, Output_File);
  312.   
  313.   if(fseek(Input_File, 1024, 0)) 
  314.   {
  315.     perror("Error on fseek");
  316.     exit(errno);
  317.   }
  318.  
  319.   do 
  320.   {
  321.     fread(&My_Byte, sizeof(unsigned char), 1, Input_File);
  322.     fwrite(&My_Byte, sizeof(unsigned char), 1, Output_File);
  323.   } while(!(feof(Input_File)));
  324.   
  325.   fclose(Output_File);
  326.   fclose(Input_File);
  327. }
  328.  
  329. --------------2290872267--
  330.  
  331.